﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BLL.Helper.Pagination
{
    public class PageResultDTO
    {
        
        //Constructor
        public PageResultDTO(int pageIndex, int pageSize, long total)
        {
            SetPage(pageIndex, pageSize, total); 
        }


        // Properties
        public bool HasNextPage { get; private set; }

        public bool HasPrevPage { get; private set; }

        public int PageIndex { get; private set; }

        public int PageSize { get; private set; }

        public int SkipRecords { get; private set; }

        public int TotalPages { get; private set; }

        public long TotalRecords { get; private set; }



        //Methods
        public void SetPage(int pageIndex, int pageSize, long total)
        {
            if (pageIndex < 1)
            {
                pageIndex = 1;
            }
            if (pageSize < 1)
            {
                pageSize = 1;
            }
            this.TotalRecords = total;
            this.PageSize = pageSize;
            this.TotalPages = (int)Math.Ceiling((double)(((double)this.TotalRecords) / ((double)this.PageSize)));
            if (this.TotalPages < 1)
            {
                this.TotalPages = 1;
            }
            this.PageIndex = (pageIndex > this.TotalPages) ? this.TotalPages : pageIndex;
            this.SkipRecords = (this.PageIndex - 1) * this.PageSize;
            this.HasPrevPage = this.SkipRecords > 0;
            this.HasNextPage = (this.TotalRecords - this.SkipRecords) > this.PageSize;
        }
    }
}
